package com.gpframework.module.controller.purchase;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.module.module.domain.req.GpIncomingInspectUpdate;
import com.gpframework.module.purchase.domain.req.GpPurchaseTrackUpdate;
import com.gpframework.module.purchase.service.IGpPurchaseQualityFlowDetailsService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.purchase.domain.GpPurchaseQualityFeedback;
import com.gpframework.module.purchase.domain.vo.GpPurchaseQualityFeedbackVo;
import com.gpframework.module.purchase.domain.req.GpPurchaseQualityFeedbackReq;
import com.gpframework.module.purchase.domain.req.GpPurchaseQualityFeedbackAdd;
import com.gpframework.module.purchase.domain.req.GpPurchaseQualityFeedbackUpdate;
import com.gpframework.module.purchase.service.IGpPurchaseQualityFeedbackService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpPurchaseQualityFeedbackController
 * @Author gpframework
 * @Date 2024-01-09
 * @Version 1.0.0
 * @Description 采购品质反馈Controller
 */
@Validated
@Api(tags = "采购品质反馈")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/purchase/qualityfeedback" )
public class GpPurchaseQualityFeedbackController extends BaseController {

    private final IGpPurchaseQualityFeedbackService iGpPurchaseQualityFeedbackService;
    private final IGpPurchaseQualityFlowDetailsService iGpPurchaseQualityFlowDetailsService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-09
  * @Param gpPurchaseQualityFeedback
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('purchase:qualityfeedback:list')")
    @GetMapping("/list")
    public TableDataInfo<GpPurchaseQualityFeedbackVo> list(GpPurchaseQualityFeedbackReq gpPurchaseQualityFeedbackReq)
    {
        startPage();
        QueryWrapper<GpPurchaseQualityFeedback> lqw = new QueryWrapper<GpPurchaseQualityFeedback>();
        gpPurchaseQualityFeedbackReq.generatorQuery(lqw,true);
        List<GpPurchaseQualityFeedbackVo> list = iGpPurchaseQualityFeedbackService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseQualityFeedback
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('purchase:qualityfeedback:add')" )
    @Log(title = "添加采购品质反馈" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpPurchaseQualityFeedbackAdd gpPurchaseQualityFeedbackAdd) {
        GpPurchaseQualityFeedback gpPurchaseQualityFeedback = Convert.convert(new TypeReference<GpPurchaseQualityFeedback>() {}, gpPurchaseQualityFeedbackAdd);
        return toAjax(iGpPurchaseQualityFeedbackService.save(gpPurchaseQualityFeedback) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseQualityFeedback
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('purchase:qualityfeedback:edit')" )
    @Log(title = "修改采购品质反馈" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpPurchaseQualityFeedbackUpdate> updateList) {
        List<GpPurchaseQualityFeedback> list = Convert.convert(new TypeReference<List<GpPurchaseQualityFeedback>>() {}, updateList);
        return toAjax(iGpPurchaseQualityFeedbackService.updateBatchById(list) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseQualityFeedback
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('purchase:qualityfeedback:export')" )
    @Log(title = "导出采购品质反馈" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpPurchaseQualityFeedbackReq gpPurchaseQualityFeedbackReq) {
        QueryWrapper<GpPurchaseQualityFeedback> lqw = new QueryWrapper<GpPurchaseQualityFeedback>();
        gpPurchaseQualityFeedbackReq.generatorQuery(lqw,true);
        List<GpPurchaseQualityFeedbackVo> list = iGpPurchaseQualityFeedbackService.listVo(lqw);
        ExcelUtil<GpPurchaseQualityFeedbackVo> util = new ExcelUtil<GpPurchaseQualityFeedbackVo>(GpPurchaseQualityFeedbackVo. class);
        return util.exportExcel(list, "qualityfeedback" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseQualityFeedback
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('purchase:qualityfeedback:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpPurchaseQualityFeedbackVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpPurchaseQualityFeedbackService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseQualityFeedback
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('purchase:qualityfeedback:remove')" )
    @Log(title = "删除采购品质反馈" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpPurchaseQualityFeedbackService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 品质问题跟踪明细
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpIncomingInspect
     * @Return TableDataInfo
     */
    @ApiOperation("品质问题跟踪明细")
    @PreAuthorize("@ss.hasPermi('purchase:qualityfeedback:add')" )
    @Log(title = "品质问题跟踪明细" , businessType = BusinessType.INSERT)
    @PostMapping("/qualityIssueTrackDetails")
    public AjaxResult qualityIssueTrackDetails(@RequestBody GpPurchaseQualityFeedbackUpdate gpPurchaseQualityFeedbackUpdate) {
        if (gpPurchaseQualityFeedbackUpdate == null || CollectionUtils.isEmpty(gpPurchaseQualityFeedbackUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpPurchaseQualityFlowDetailsService.qualityIssueTrackDetails(gpPurchaseQualityFeedbackUpdate) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('purchase:qualityfeedback:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpPurchaseQualityFeedbackUpdate gpPurchaseQualityFeedbackUpdate) {
        if (gpPurchaseQualityFeedbackUpdate == null || gpPurchaseQualityFeedbackUpdate.getId() == null || gpPurchaseQualityFeedbackUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpPurchaseQualityFeedbackUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpPurchaseQualityFeedbackService.copy(gpPurchaseQualityFeedbackUpdate.getId(), gpPurchaseQualityFeedbackUpdate.getCopyNum()) ? 1 : 0);
    }
}
